package sqlconnection;

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Scanner;

public class SqlConnect {

	public static String doQuery(String query) throws SQLException
	{
		Connection con;
		 
		try {
 
			String url = "jdbc:oracle:thin:@dbvm07.iai.uni-bonn.de:1521:lehre";
			con = DriverManager.getConnection(url, "s6morehb", "squeueL5ux");
			con.setAutoCommit(false);
			con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
		

		
		} catch (SQLException e) {
			
			System.out.println("Connection Failed! Check output console");
			e.printStackTrace();
			return "Connection Error";
 
		}
		try {
			Statement stmt = con.createStatement(
				ResultSet.TYPE_SCROLL_SENSITIVE,
				ResultSet.CONCUR_UPDATABLE,
				ResultSet.HOLD_CURSORS_OVER_COMMIT);
				ResultSet rs = stmt.executeQuery(query);
				ResultSetMetaData rsm = rs.getMetaData();
				String resultpile = ""; 
				int colnum = rsm.getColumnCount();
				for (int i=1; i<=colnum; i++)
				{
					while (rs.next()) {
						String content = rs.getString(i);
						resultpile = resultpile + content;
					}
					rs.first();
				}
				rs.close();
				return resultpile;
		}
		catch (SQLException e) {
			String url = "jdbc:oracle:thin:@dbvm07.iai.uni-bonn.de:1521:lehre";
			con = DriverManager.getConnection(url, "s6morehb", "squeueL5ux");
			con.rollback();
			return "SQL Exception";
			}
	}
	public static String getQuery() {
        Scanner sc = new Scanner(System.in);
        String test = sc.nextLine();
        System.out.println("Query is "+test);
        sc.close();
        return test;
    }
}
